//index.js
//获取应用实例
const baseUrl = require('../../config').baseUrl
const app = getApp()

Page({
    data: {
        isBackstage: false, // 是否后台
        selectImg: "../../images/select.png",
        selectedImg: "../../images/selected.png",
        isSelect: false,
        selMsg: '请选择查询单位',
        companyList: [{ departname: '全部', id: '' }], // 获取单位列表
        avatarUrl: '',
        scrollTop: 0,
        hiddenList: [],
        pageNum: 0, // 总页数
        count: 0,
        currentPageIndex: 1, // 当前在第几页
        loading: false,
        showRefreshText: true
    },

    /**
  * 生命周期函数--监听页面加载
  */
    onLoad: function (options) {
        this.setData({
            avatarUrl: wx.getStorageSync('userInfo').avatarUrl
        })
        wx.showLoading({
            title: '加载中',
        })
        this.getHiddenList()
        this.getCompany()
    },
    onHide: function () {
        this.setData({
            isSelect: false,
            isBackstage: true,
            hiddenList: []
        })
    },
    onShow () {
        if (this.data.isBackstage) {
            this.setData({
                companyList: [{ departname: '全部', id: '' }],
                isBackstage: false,
                pageNum: 0, // 总页数
                currentPageIndex: 1 // 当前在第几页
            })
            wx.showLoading({
                title: '加载中',
            })
            this.getHiddenList()
            this.getCompany()
        }
    },
    /**
    *  获取单位列表
    */
    getCompany () {
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "GetDeptInfoList",
                    data: {
                        account: app.globalData.account
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    This.setData({
                        companyList: This.data.companyList.concat(res.data.data)
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
    bindShowMsg: function () {
        this.setData({
            isSelect: !this.data.isSelect
        })
    },
    mySelect: function (e) {
        let name = e.currentTarget.dataset.name
        let companyId = e.currentTarget.dataset.companyid
        this.setData({
            selMsg: name,
            selectedCompanyId: companyId,
            isSelect: false,
            currentPageIndex: 1, // 当前在第几页
            hiddenList: []
        })
        wx.showLoading({
            title: '加载中',
        })
        this.getHiddenList()
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    gotoDetail: function (e) {
        var hideId = e.currentTarget.dataset.hideid
        wx.navigateTo({
            url: `../hiddenaccountdetail/hiddenaccountdetail?hideId=${hideId}&page=2`
        })
    },
    gotoUser: function () {
        wx.switchTab({
            url: '/pages/userinfo/userinfo'
        })
    },
    /**
     * 获取隐患台账列表
     */
    getHiddenList (refresh) {
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "GetHiddenList",
                    data: {
                        isresolved: 1,
                        account: app.globalData.account, // 用户账号
                        deptid: this.data.selectedCompanyId, // 检查单位ID
                        pageindex: this.data.currentPageIndex, // 页数
                        pagesize: '10' // 每页显示行数

                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    This.setData({
                        hiddenList: This.data.hiddenList.concat(res.data.data),
                        pageNum: Math.ceil(res.data.count / 10),
                        count: res.data.count
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            },
            complete: () => {
                wx.hideLoading()
                This.setData({
                    loading: false
                })

                if (refresh) {
                    This.setData({
                        'showRefreshText': true
                    })
                }
            }
        })
    },
    /**
     * 上拉加载
     */
    upPullLoad (e) {
        if (!this.data.loading && this.data.currentPageIndex < this.data.pageNum) {
            this.setData({
                loading: true,
                currentPageIndex: this.data.currentPageIndex + 1
            })
            this.getHiddenList()
        }
    },
    /**
     * 计算上拉的距离
     */
    scrollMove (e) {
        if (!this.data.loading) {
            this.setData({
                scrollTop: e.detail.dy
            })
        }
    },
    scrollTouchend () {
        if (this.data.scrollTop <= -40 && !this.data.loading) {
            this.setData({
                scrollTop: 0,
                hiddenList: [],
                pageNum: 0, // 总页数
                currentPageIndex: 1, // 当前在第几页
                loading: true,
                showRefreshText: false
            }, () => {
                this.getHiddenList('refresh')
            })
        }
    }
})
